package cn.wolfcode.crm.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMultEmail extends Thread {
	//用于给用户发送邮件的邮箱
		private String from = "1258584952@qq.com";
	      //邮箱的用户名
		private String username = "1258584952";
	      //邮箱的密码
	//  private String password = "邮箱密码";
		private String password = "uwpycgchjmxfbabd";
	      //发送邮件的服务器地址
	  private String host = "smtp.qq.com";
	  
	  private String info = "";
	  
	  //邮件地址list
	  private List<String> address = new ArrayList<String>();
	  
	  public List<String> getAddress() {
		return address;
	}


	public void setAddress(List<String> address) {
		this.address = address;
	}


	public String getInfo() {
		return info;
	  }


	public void setInfo(String info) {
		this.info = info;
	} 


	/* 重写run方法的实现，在run方法中发送邮件给指定的用户
	   * @see java.lang.Thread#run()
	   */
	  @Override
	  public void run() {
		  
	      try{
	          Properties prop = new Properties();
	          prop.setProperty("mail.host", host);
	          prop.setProperty("mail.transport.protocol", "smtp");
	          prop.setProperty("mail.smtp.auth", "true");
	          //使用JavaMail发送邮件的5个步骤
	          //1、创建session
	          Session session = Session.getInstance(prop);
	        //开启Session的debug模式，这样就可以查看到程序发送Email的运行状态
	          session.setDebug(true);
	        //2、通过session得到transport对象
	          Transport ts = session.getTransport();
	        //3、使用邮箱的用户名和密码连上邮件服务器，发送邮件时，
	          //发件人需要提交邮箱的用户名和密码给SMTP服务器，
	          //用户名和密码都通过验证之后才能够正常发送邮件给收件人。
	          ts.connect(host, username, password);
	        //4、创建邮件
	          Message message = createEmail(session,info);
	        //5、发送邮件
	          ts.sendMessage(message, message.getAllRecipients());
	          ts.close();
	      }catch (Exception e) {
	          throw new RuntimeException(e);
	      }
	  }
	  


	/**
	  * @Method: createEmail
	  * @Description: 创建要发送的邮件 只有文本
	  *
	  * @param session
	  * @param info
	  * @return
	  * @throws Exception
	  */ 
	  public Message createEmail(Session session,String info) throws Exception{
		   //创建邮件对象
	      MimeMessage message = new MimeMessage(session);
	      //指明邮件的发件人
	      message.setFrom(new InternetAddress(from));
	    //指明邮件的收件人，现在发件人和收件人是一样的，那就是自己给自己发
	      InternetAddress[] sendTo = new InternetAddress[address.size()];  
	      for (int i = 0; i < address.size(); i++) {  
	          System.out.println("发送到:" + address.get(i));  
	          sendTo[i] = new InternetAddress(address.get(i));  
	      }  
//	      message.setRecipients(Message.RecipientType.TO, sendTo);
	      message.setRecipients(javax.mail.internet.MimeMessage.RecipientType.TO,  
	              sendTo); 
	    //邮件的标题
	      message.setSubject("用户通知邮件");
	    //邮件的文本内容 info
//	      info = "恭喜您注册成功，您的用户名：" + user.getName()+ ",您的密码：" + user.getPassword() + "，请妥善保管，如有问题请联系网站客服!!";
	      message.setContent(info, "text/html;charset=UTF-8");
	      message.saveChanges();
	    //返回创建好的邮件对象
	      return message;
	  }
	}
